<template>
  <div class="layout-col flex5 bss ml16">
    <div class="ww100 flex-bc plr12 ptb8 bs4">
      <div class="flex-sc">
        <span class="f15 fw">{{isNull(publicStore.active)?'':publicStore.active.name}}</span>
      </div>
      <div class="flex-ec flex1 h30">
        <div class="rad4 ptb6 plr10 flex-cc cursor bgi3 white ml10" 
          v-if="state.content.find(v=>{return typeof(v.key) == 'object'}) && state.content.find(v=>{return typeof(v.key) == 'object'})['key'].hasOwnProperty('add')" 
          @click.stop="handleClick('add', state.addItem)">
          <i-ep-plus class="f12 fw" /><span class="f14 ml5">{{state.content.find(v=>{return typeof(v.key) == 'object'})['key'].add}}</span>
        </div>
      </div>
    </div>
    <div class="row bgi22">
      <div :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i" v-show="v.show" :class="typeof(v.key) == 'object'?v.width +' '+ 'mlr5':v.width">{{v.name}}</div>
    </div>
    <div class="table">
      <div class="row" :class="publicStore.active[state.key] == item[state.key]?'active':index%2 == 1?'bgi23':'bgi18'" v-for="(item, index) in state.list" :key="index" @click="handleClick('active', item)">
        <div v-show="v.show" :class="v.width" :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i">
          <span v-if="typeof(v.key) == 'object'" class="i21 cursor mlr5" :class="kk=='add'?'none':kk=='important'&&item['area']==1?'none':''" v-for="(vv, kk, ii) in v.key" :key="ii" @click.stop="handleClick(kk, item)">{{vv}}</span>
           <span v-else-if="v.key=='*'">{{index+1}}</span>
          <span v-else-if="v.type == 'time'">{{item[v.key] == 0?'/':parseTime(item[v.key])}}</span>
          <span v-else-if="v.type == 'find'" :class="find(v.value[0], [v.value[1], item[v.key]], 'color')?find(v.value[0], [v.value[1], item[v.key]], 'color'):''">
            {{find(v.value[0], [v.value[1], item[v.key]], v.value[2])}}
          </span>
          <span v-else-if="v.type == 'switch'">
            <el-switch v-model="item[v.key]" :active-text="v.value[1].name" :inactive-text="v.value[0].name" :active-value="v.value[1].value" :inactive-value="v.value[0].value" inline-prompt @change="handleClick('switch', item)"></el-switch>
          </span>
          <span v-else-if="v.type == 'input'"><el-input v-model="item[v.value]" placeholder="请输入" style="width: 150px;" size="small" /></span>
          <span v-else>{{isNull(item[v.key]) && v.key!=''? '/' : item[v.key]}}</span>
        </div>
      </div>
      <div v-if="state.empty" class="flex-cc ww100 tc ptb13 bgi18">没有数据</div>
    </div>
    <Add @init="init" :state="state" ref="exposeRef" />
  </div>
</template>

<script lang="ts" setup>
  import api from '@/api'
  import Add from '@/components/Add'
	const { proxy }:any = getCurrentInstance()
  const publicStore = proxy.publicStore()
  const configStore = proxy.configStore()
  const state = reactive({
	  ...publicStore.$state,
    key: 'station_num',
    query: {"o":"o_comm_station", "p":[], "i":0},
    content: [
      { width: 'w70',   show: true, align: 'center', key: '*', name: '序号' },
      { width: 'w50x5', show: true, align: 'left', key: 'station_name', name: '厂站名称' }, 
      { width: 'w50x5', show: true, align: 'left', key: 'station_num', name: '厂站编号' },
      { width: 'w50x5', show: proxy.isNull(configStore.config.main)? false : true, align: 'left', key: 'ip', name: '厂站来源' },
      { width: 'flex1', show: true, align: 'right', key: {add: '新增', important:'重点', edit:'编辑', del: '删除'}, name: '操作' },
    ],
    editField: {},
    editFrom: [
      { required: true, editshow: true,  name: '区域', key: 'up_org_id', type: ['tree', [], 'id', 'up_id', '顶级'] },
      { required: true, editshow: true,  name: '编号', key: 'station_num', type: ['input'] },
      { required: true, editshow: true,  name: '名称', key: 'station_name', type: ['input'] },
      { required: false, editshow: true,  name: '来源', key: 'ip', type: ['input'] },
    ],
    addItem: {},
  })
  let exposeRef = $ref()

  onMounted(async() => {
    if(proxy.isNull(configStore.config.main)) state.editFrom.splice(3, 1)
  })

  watch(() => publicStore.active, async(val) => {
    await nextTick()
    if(proxy.isNull(val)) return
    init()
  }, {immediate: false, deep: true})

  const setInit = () => {
    state.editFrom.forEach(v => {
      if(v.key == 'up_org_id') v.type[1] = publicStore._public.list
    })
    state.addItem = {up_org_id: publicStore.active.id}
  }

  const init = async() => {
    state.params = {queryApi: getQuery(publicStore.active)}
    let res = await publicStore.post(state.params)
    state.empty = proxy.isNull(res)? true : false
    state.list = proxy.isNull(res)? [] : res
    setInit()
  }

  const getQuery = (val, tempArr=[]) => {
    if(proxy.isNull(val.children)){
      let temp = Object.assign({}, state.query)
      temp.p = [val.id]
      tempArr.push(temp)
    }else{
      val.children.forEach(v => {
        getQuery(v, tempArr)
      })
    }
    return tempArr
  }

  const handleClick = async(remark, val) => {
    if(remark == 'add' || remark == 'edit'){
      exposeRef.onVisable(val)
    }
    if(remark == 'del'){
      exposeRef.del(val, state.query, state.key)
    }
    if(remark == 'important'){
      let query = {"o": "o_comm_station"}
      let params = {queryApi: query}
      let res = await publicStore.post(params)
      if(proxy.isNull(res)) return
      let allForm = []
      allForm.push({"o": "o_comm_station", "t": "upd", "a": "修改", station_num: val.station_num, area: '1'})
      let exist = res.find(a=>a.area == 1)
      if(exist) allForm.push({"o": "o_comm_station", "t": "upd", "a": "修改", station_num: exist.station_num, area: ''})
      console.log('allForm', allForm)
      api.updateApi(allForm).then((res:any) => {
        if(res.errcode == 0){
          init()
          ElNotification({ title: '提示', message: '操作成功', type: 'success' })
        }
      }).catch((err) => {
        ElNotification({ title: '提示', message: '操作失败', type: 'error' })
      })
    }
  }

</script>
  
<style scoped lang="scss">

</style>
  